package com.snowflake.ecatalog.service.profile;

import java.util.List;
import java.util.Map;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import com.google.common.collect.Lists;
import com.google.common.collect.Maps;
import com.snowflake.ecatalog.entity.ResourceInfo;
import com.snowflake.ecatalog.repository.ResourceInfoDao;
import com.snowflake.ecatalog.util.FileOperateUtil;

@Service
@Transactional(readOnly = true)
public class ResouceInfoService {
	@Autowired
	private ResourceInfoDao resourceInfoDao;

	@Transactional(readOnly = false)
	public void saveResourceInfo(List<ResourceInfo> resourceInfoList) {
		resourceInfoDao.save(resourceInfoList);
	}

	@Transactional(readOnly = false)
	public void deleteResourceInfoFile(Long ownerId, int ownerType) {
		List<ResourceInfo> resourceInfoList = resourceInfoDao.findByOwnerIdAndOwnerType(ownerId, ownerType);
		deleteResourceInfoFile(resourceInfoList);
	}

	@Transactional(readOnly = false)
	public void deleteResourceInfoFile(List<ResourceInfo> resourceInfoList) {
		FileOperateUtil.delete(resourceInfoList);
		resourceInfoDao.delete(resourceInfoList);
	}

	// 更新ResourceInfo，保存新资源，删除无效资源
	@Transactional(readOnly = false)
	public void updateResourceInfo(Long ownerId, int ownerType, List<ResourceInfo> resourceInfoList) {
		List<ResourceInfo> addNewResourceInfoList = Lists.newArrayList();
		List<Long> retainResourceInfoIdList = Lists.newArrayList();
		for (ResourceInfo resourceInfo : resourceInfoList) {
			if (resourceInfo.getId() == null) {
				resourceInfo.setOwnerId(ownerId);
				resourceInfo.setOwnerType(ownerType);
				addNewResourceInfoList.add(resourceInfo);
			} else {
				retainResourceInfoIdList.add(resourceInfo.getId());
			}
		}
		// 删除无效的资源路径
		deleteInvalidResourceInfo(ownerId, ownerType, retainResourceInfoIdList);
		saveResourceInfo(addNewResourceInfoList);
	}

	// 删除无效的资源路径
	private void deleteInvalidResourceInfo(Long ownerId, int ownerType, List<Long> retainResourceInfoIdList) {
		List<ResourceInfo> oldResourceInfoList = findByOwnerIdAndOwnerType(ownerId, ownerType);
		List<ResourceInfo> deleteResourceInfoList = Lists.newArrayList();
		for (ResourceInfo resourceInfo : oldResourceInfoList) {
			if (!retainResourceInfoIdList.contains(resourceInfo.getId())) {
				deleteResourceInfoList.add(resourceInfo);
			}
		}
		// deleteResourceInfoFile(deleteResourceInfoList);
		// 因为文件名都相同，修改的时候，不能删除文件
		resourceInfoDao.delete(deleteResourceInfoList);
	}

	public List<ResourceInfo> findByOwnerIdAndOwnerType(Long ownerId, int ownerType) {
		return resourceInfoDao.findByOwnerIdAndOwnerType(ownerId, ownerType);
	}

	public Map<Integer, ResourceInfo> getImgPathMap(Long ownerId, int ownerType) {
		List<ResourceInfo> resourceInfoList = resourceInfoDao.findByOwnerIdAndOwnerType(ownerId, ownerType);
		Map<Integer, ResourceInfo> imgPathMap = Maps.newHashMap();
		for (ResourceInfo resourceInfo : resourceInfoList) {
			imgPathMap.put(resourceInfo.getSequence(), resourceInfo);
		}
		return imgPathMap;
	}
}
